####################################
# RDD plots gender other bandwidths
####################################

rm(list=ls())

library(Hmisc)
library(ggplot2)
library(stargazer)
library(foreign)
library(rdrobust)
library(rdd)
library(readstata13)
library(gridExtra)

################
# Prepare data 
################

# read data
load("~/Dropbox/Gender Chile/08_replication/gender_chile_2020aug28.RData")  
names(d)

#########################
# Bandwidth 15
#########################

# Bandwidth 0.15
summary(rdrobust(d$permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.15))
summary(rdrobust(d$female_permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.15))
summary(rdrobust(d$male_permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.15))
summary(rdrobust(d$sharefemale_permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.15))

# plots
(rdplot(y = d$permanent_temporary_s, x = d$margin,  h= 0.15, nbins = 1000, subset = -0.15 <= d$margin & d$margin <= 0.15, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(-1, 1), title = "Robust CI: [-265.718,-73.159]",
        y.label = "Municipal employees", x.label = "Women candidates' margin of victory"))


(rdplot(y = d$female_permanent_temporary_s, x = d$margin,  h= 0.15, nbins = 1000, subset = -0.15 <= d$margin & d$margin <= 0.15, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(-1, 1), title = "Robust CI: [-97.775,-17.895]",
        y.label = "Women municipal employees", x.label = "Women candidates' margin of victory"))

(rdplot(y = d$male_permanent_temporary_s, x = d$margin,  h= 0.15, nbins = 1000, subset = -0.15 <= d$margin & d$margin <= 0.15, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(-1, 1), title = "Robust CI: [-168.036,-55.171]",
        y.label = "Men municipal employees", x.label = "Women candidates' margin of victory"))


(rdplot(y = d$sharefemale_permanent_temporary, x = d$margin,  h= 0.15, nbins = 1000, subset = -0.15 <= d$margin & d$margin <= 0.15, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(0, 1), title = "Robust CI: [0.039,0.149]",
        y.label = "Share of women employees", x.label = "Women candidates' margin of victory"))

#########################
# Bandwidth 14
#########################

# Bandwidth 0.14
summary(rdrobust(d$permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.14))
summary(rdrobust(d$female_permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.14))
summary(rdrobust(d$male_permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.14))
summary(rdrobust(d$sharefemale_permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.14))

# plots
(rdplot(y = d$permanent_temporary_s, x = d$margin,  h= 0.14, nbins = 1000, subset = -0.14 <= d$margin & d$margin <= 0.14, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(-1, 1), title = "Robust CI: [-276.720,-76.217]",
        y.label = "Municipal employees", x.label = "Women candidates' margin of victory"))

(rdplot(y = d$female_permanent_temporary_s, x = d$margin,  h= 0.14, nbins = 1000, subset = -0.14 <= d$margin & d$margin <= 0.14, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(-1, 1), title = "Robust CI: [-102.260,-19.026]",
        y.label = "Women municipal employees", x.label = "Women candidates' margin of victory"))

(rdplot(y = d$male_permanent_temporary_s, x = d$margin,  h= 0.14, nbins = 1000, subset = -0.14 <= d$margin & d$margin <= 0.14, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(-1, 1), title = "Robust CI: [-174.549,-57.101]",
        y.label = "Men municipal employees", x.label = "Women candidates' margin of victory"))

(rdplot(y = d$sharefemale_permanent_temporary, x = d$margin,  h= 0.14, nbins = 1000, subset = -0.14 <= d$margin & d$margin <= 0.14, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(0, 1), title = "Robust CI: [0.041,0.156]",
        y.label = "Share of women employees", x.label = "Women candidates' margin of victory"))

#########################
# Bandwidth 13
#########################

# Bandwidth 0.13
summary(rdrobust(d$permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.13))
summary(rdrobust(d$female_permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.13))
summary(rdrobust(d$male_permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.13))
summary(rdrobust(d$sharefemale_permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.13))

# plots
(rdplot(y = d$permanent_temporary_s, x = d$margin,  h= 0.13, nbins = 1000, subset = -0.13 <= d$margin & d$margin <= 0.13, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(-1, 1), title = "Robust CI: [-288.920 , -74.001]",
        y.label = "Municipal employees", x.label = "Women candidates' margin of victory"))

(rdplot(y = d$female_permanent_temporary_s, x = d$margin,  h= 0.13, nbins = 1000, subset = -0.13 <= d$margin & d$margin <= 0.13, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(-1, 1), title = "Robust CI: [-107.809,-18.497]",
        y.label = "Women municipal employees", x.label = "Women candidates' margin of victory"))

(rdplot(y = d$male_permanent_temporary_s, x = d$margin,  h= 0.13, nbins = 1000, subset = -0.13 <= d$margin & d$margin <= 0.13, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(-1, 1), title = "Robust CI: [-181.192,-55.422]",
        y.label = "Men municipal employees", x.label = "Women candidates' margin of victory"))

(rdplot(y = d$sharefemale_permanent_temporary, x = d$margin,  h= 0.13, nbins = 1000, subset = -0.13 <= d$margin & d$margin <= 0.13, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(0, 1), title = "Robust CI: [0.039,0.163]",
        y.label = "Share of women employees", x.label = "Women candidates' margin of victory"))

#########################
# Bandwidth 12
#########################

# Bandwidth 0.12
summary(rdrobust(d$permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.12))
summary(rdrobust(d$female_permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.12))
summary(rdrobust(d$male_permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.12))
summary(rdrobust(d$sharefemale_permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.12))

# plots
(rdplot(y = d$permanent_temporary_s, x = d$margin,  h= 0.12, nbins = 1000, subset = -0.12 <= d$margin & d$margin <= 0.12, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(-1, 1), title = "Robust CI: [-302.502,-73.771]",
        y.label = "Municipal employees", x.label = "Women candidates' margin of victory"))

(rdplot(y = d$female_permanent_temporary_s, x = d$margin,  h= 0.12, nbins = 1000, subset = -0.12 <= d$margin & d$margin <= 0.12, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(-1, 1), title = "Robust CI: [-114.469,-19.278]",
        y.label = "Women municipal employees", x.label = "Women candidates' margin of victory"))

(rdplot(y = d$male_permanent_temporary_s, x = d$margin,  h= 0.12, nbins = 1000, subset = -0.12 <= d$margin & d$margin <= 0.12, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(-1, 1), title = "Robust CI: [-188.108,-54.418]",
        y.label = "Men municipal employees", x.label = "Women candidates' margin of victory"))

(rdplot(y = d$sharefemale_permanent_temporary, x = d$margin,  h= 0.12, nbins = 1000, subset = -0.12 <= d$margin & d$margin <= 0.12, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(0, 1), title = "Robust CI: [0.040,0.173]",
        y.label = "Share of women employees", x.label = "Women candidates' margin of victory"))

#########################
# Bandwidth 11
#########################

# Bandwidth 0.11
summary(rdrobust(d$permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.11))
summary(rdrobust(d$female_permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.11))
summary(rdrobust(d$male_permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.11))
summary(rdrobust(d$sharefemale_permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.11))

# plots
(rdplot(y = d$permanent_temporary_s, x = d$margin,  h= 0.11, nbins = 1000, subset = -0.11 <= d$margin & d$margin <= 0.11, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(-1, 1), title = "Robust CI: [-309.433,-67.546]",
        y.label = "Municipal employees", x.label = "Women candidates' margin of victory"))

(rdplot(y = d$female_permanent_temporary_s, x = d$margin,  h= 0.11, nbins = 1000, subset = -0.11 <= d$margin & d$margin <= 0.11, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(-1, 1), title = "Robust CI: [-118.441,-17.696]",
        y.label = "Women municipal employees", x.label = "Women candidates' margin of victory"))

(rdplot(y = d$male_permanent_temporary_s, x = d$margin,  h= 0.11, nbins = 1000, subset = -0.11 <= d$margin & d$margin <= 0.11, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(-1, 1), title = "Robust CI: [-191.061,-49.782]",
        y.label = "Men municipal employees", x.label = "Women candidates' margin of victory"))

(rdplot(y = d$sharefemale_permanent_temporary, x = d$margin,  h= 0.11, nbins = 1000, subset = -0.11 <= d$margin & d$margin <= 0.11, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(0, 1), title = "Robust CI: [0.040,0.182]",
        y.label = "Share of women employees", x.label = "Women candidates' margin of victory"))

#########################
# Bandwidth 10
#########################

# Bandwidth 0.10
summary(rdrobust(d$permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.10))
summary(rdrobust(d$female_permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.10))
summary(rdrobust(d$male_permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.10))
summary(rdrobust(d$sharefemale_permanent_temporary,d$margin,cluster = d$cluster,all=TRUE,h=0.10))

# plots
(rdplot(y = d$permanent_temporary_s, x = d$margin,  h= 0.10, nbins = 1000, subset = -0.10 <= d$margin & d$margin <= 0.10, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(-1, 1), title = "Robust CI: [-301.451,-52.335]",
        y.label = "Municipal employees", x.label = "Women candidates' margin of victory"))

(rdplot(y = d$female_permanent_temporary_s, x = d$margin,  h= 0.10, nbins = 1000, subset = -0.10 <= d$margin & d$margin <= 0.10, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(-1, 1), title = "Robust CI: [-117.193,-13.373]",
        y.label = "Women municipal employees", x.label = "Women candidates' margin of victory"))

(rdplot(y = d$male_permanent_temporary_s, x = d$margin,  h= 0.10, nbins = 1000, subset = -0.10 <= d$margin & d$margin <= 0.10, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(-1, 1), title = "Robust CI: [-184.320,-38.899]",
        y.label = "Men municipal employees", x.label = "Women candidates' margin of victory"))


(rdplot(y = d$sharefemale_permanent_temporary, x = d$margin,  h= 0.10, nbins = 1000, subset = -0.10 <= d$margin & d$margin <= 0.10, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(0, 1), title = "Robust CI: [0.037,0.183]",
        y.label = "Share of women employees", x.label = "Women candidates' margin of victory"))
